import { UseRules } from './useRule';
let _UseRules = UseRules;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import UseRuleMock from './mk/mng/card/useRuleMock';
 * UseRuleMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/card/useRule/listKey').reply(config => {
      let params = config.params;
      let mockUseRules = _UseRules.filter(useRule => {
      	if(params.acceptCategory||params.acceptCategory==''){ if( useRule.acceptCategory.indexOf(params.acceptCategory)>=0) return true;}
      	if(params.rejectCategory||params.rejectCategory==''){ if( useRule.rejectCategory.indexOf(params.rejectCategory)>=0) return true;}
      	if(params.leastCost||params.leastCost==''){ if( useRule.leastCost.indexOf(params.leastCost)>=0) return true;}
      	if(params.objectUseFor||params.objectUseFor==''){ if( useRule.objectUseFor.indexOf(params.objectUseFor)>=0) return true;}
      	if(params.canUseWithOtherDiscount||params.canUseWithOtherDiscount==''){ if( useRule.canUseWithOtherDiscount.indexOf(params.canUseWithOtherDiscount)>=0) return true;}
      	if(params.id||params.id==''){ if( useRule.id.indexOf(params.id)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( useRule.cardPkId.indexOf(params.cardPkId)>=0) return true;}
      	if(params.acceptCategoryIds||params.acceptCategoryIds==''){ if( useRule.acceptCategoryIds.indexOf(params.acceptCategoryIds)>=0) return true;}
      	if(params.rejectCategoryIds||params.rejectCategoryIds==''){ if( useRule.rejectCategoryIds.indexOf(params.rejectCategoryIds)>=0) return true;}
      	if(params.acceptGoodsIds||params.acceptGoodsIds==''){ if( useRule.acceptGoodsIds.indexOf(params.acceptGoodsIds)>=0) return true;}
      	if(params.rejectGoodsIds||params.rejectGoodsIds==''){ if( useRule.rejectGoodsIds.indexOf(params.rejectGoodsIds)>=0) return true;}
      	if(params.acceptSysIds||params.acceptSysIds==''){ if( useRule.acceptSysIds.indexOf(params.acceptSysIds)>=0) return true;}
      	if(params.acceptDeptids||params.acceptDeptids==''){ if( useRule.acceptDeptids.indexOf(params.acceptDeptids)>=0) return true;}
      	if(params.acceptGoods||params.acceptGoods==''){ if( useRule.acceptGoods.indexOf(params.acceptGoods)>=0) return true;}
      	if(params.rejectGoods||params.rejectGoods==''){ if( useRule.rejectGoods.indexOf(params.rejectGoods)>=0) return true;}
      	if(params.acceptSys||params.acceptSys==''){ if( useRule.acceptSys.indexOf(params.acceptSys)>=0) return true;}
      	if(params.acceptDept||params.acceptDept==''){ if( useRule.acceptDept.indexOf(params.acceptDept)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockUseRules
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/card/useRule/list').reply(config => {
      let params = config.params;
      let mockUseRules = _UseRules.filter(useRule => {
      	var isTrue=false;
      	if(params.acceptCategory||params.acceptCategory==''){if(useRule.acceptCategory.indexOf(params.acceptCategory)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.rejectCategory||params.rejectCategory==''){if(useRule.rejectCategory.indexOf(params.rejectCategory)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.leastCost||params.leastCost==''){if(useRule.leastCost.indexOf(params.leastCost)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.objectUseFor||params.objectUseFor==''){if(useRule.objectUseFor.indexOf(params.objectUseFor)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canUseWithOtherDiscount||params.canUseWithOtherDiscount==''){if(useRule.canUseWithOtherDiscount.indexOf(params.canUseWithOtherDiscount)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(useRule.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(useRule.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptCategoryIds||params.acceptCategoryIds==''){if(useRule.acceptCategoryIds.indexOf(params.acceptCategoryIds)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.rejectCategoryIds||params.rejectCategoryIds==''){if(useRule.rejectCategoryIds.indexOf(params.rejectCategoryIds)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptGoodsIds||params.acceptGoodsIds==''){if(useRule.acceptGoodsIds.indexOf(params.acceptGoodsIds)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.rejectGoodsIds||params.rejectGoodsIds==''){if(useRule.rejectGoodsIds.indexOf(params.rejectGoodsIds)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptSysIds||params.acceptSysIds==''){if(useRule.acceptSysIds.indexOf(params.acceptSysIds)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptDeptids||params.acceptDeptids==''){if(useRule.acceptDeptids.indexOf(params.acceptDeptids)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptGoods||params.acceptGoods==''){if(useRule.acceptGoods.indexOf(params.acceptGoods)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.rejectGoods||params.rejectGoods==''){if(useRule.rejectGoods.indexOf(params.rejectGoods)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptSys||params.acceptSys==''){if(useRule.acceptSys.indexOf(params.acceptSys)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.acceptDept||params.acceptDept==''){if(useRule.acceptDept.indexOf(params.acceptDept)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockUseRules
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockUseRules.length;
      mockUseRules = mockUseRules.filter((useRule, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockUseRules,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/card/useRule/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _UseRules = _UseRules.filter(useRule => useRule.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/card/useRule/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _UseRules = _UseRules.filter(useRule => !params.includes(useRule.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/card/useRule/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { acceptCategory,rejectCategory,leastCost,objectUseFor,canUseWithOtherDiscount,id,cardPkId,acceptCategoryIds,rejectCategoryIds,acceptGoodsIds,rejectGoodsIds,acceptSysIds,acceptDeptids,acceptGoods,rejectGoods,acceptSys,acceptDept } = params;
      _UseRules.some(useRule => {
        if (useRule.id === id) {
			useRule.acceptCategory = acceptCategory;
			useRule.rejectCategory = rejectCategory;
			useRule.leastCost = leastCost;
			useRule.objectUseFor = objectUseFor;
			useRule.canUseWithOtherDiscount = canUseWithOtherDiscount;
			useRule.id = id;
			useRule.cardPkId = cardPkId;
			useRule.acceptCategoryIds = acceptCategoryIds;
			useRule.rejectCategoryIds = rejectCategoryIds;
			useRule.acceptGoodsIds = acceptGoodsIds;
			useRule.rejectGoodsIds = rejectGoodsIds;
			useRule.acceptSysIds = acceptSysIds;
			useRule.acceptDeptids = acceptDeptids;
			useRule.acceptGoods = acceptGoods;
			useRule.rejectGoods = rejectGoods;
			useRule.acceptSys = acceptSys;
			useRule.acceptDept = acceptDept
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/card/useRule/add').reply(config => {
      var params = JSON.parse( config.data );
      let { acceptCategory,rejectCategory,leastCost,objectUseFor,canUseWithOtherDiscount,id,cardPkId,acceptCategoryIds,rejectCategoryIds,acceptGoodsIds,rejectGoodsIds,acceptSysIds,acceptDeptids,acceptGoods,rejectGoods,acceptSys,acceptDept } = params;
      _UseRules.push({
		acceptCategory:acceptCategory,
		rejectCategory:rejectCategory,
		leastCost:leastCost,
		objectUseFor:objectUseFor,
		canUseWithOtherDiscount:canUseWithOtherDiscount,
		id:id,
		cardPkId:cardPkId,
		acceptCategoryIds:acceptCategoryIds,
		rejectCategoryIds:rejectCategoryIds,
		acceptGoodsIds:acceptGoodsIds,
		rejectGoodsIds:rejectGoodsIds,
		acceptSysIds:acceptSysIds,
		acceptDeptids:acceptDeptids,
		acceptGoods:acceptGoods,
		rejectGoods:rejectGoods,
		acceptSys:acceptSys,
		acceptDept:acceptDept
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};